/**
 * Created by LeroyMin on 2016/12/11.
 */

/**
 * 生成菜单
 */
function createMenu(data){
    var menuNav = document.getElementById("menu1");
    for(var i=0;i<data.length;i++){
            var name = data[i].m_cn;
            var url = data[i].m_url;
            var pNode = data[i].m_parent;
            var node = data[i].m_node;
        if(pNode == 0 || pNode == null){
            var ul = document.createElement("ul");
            var a = document.createElement("a");
            a.setAttribute("onclick","openMenu(this);return false;");
            a.setAttribute("href","javaScript:void(0)");
            a.innerText = name;
            ul.setAttribute("id",node);
            ul.appendChild(a);
            menuNav.appendChild(ul);
        } else{
            var li = document.createElement("li");
            var a = document.createElement("a");
            li.setAttribute("id",node);
            li.setAttribute("class","hidden");
            a.setAttribute("href",url);
            a.setAttribute("onclick","getLink(this);return false;")
            a.innerText = name;
            li.appendChild(a);
            var currUl = document.getElementById(pNode);
            currUl.appendChild(li);
        }

    }

}
/**
 * 取得菜单
 */
function getMenu(){
    $.ajax({
        url:"../../menu/get",
        type:"GET",
        dataType:"json",
        success:function(data){
            createMenu(data);
        }
    });
}

/**
 * 打开菜单
 */
function openMenu(obj){
    // var id = obj.id;
    // var theId = document.getElementById(id);
    var ul = obj.parentNode;
    var lis = ul.getElementsByTagName("li");
    for(var i = 0;i < lis.length;i++){
        if( lis[i].getAttribute("class") == "show"){
            lis[i].setAttribute("class","hidden");
        }else{
            lis[i].setAttribute("class","show");
        }
    }
    // alert(obj.id);

}
/**
 * 打开连接
 * @param url
 */
function getLink(obj) {
    var url = obj.getAttribute("href");
    var tab = document.getElementById("tab");
    var context = document.getElementById("context");
    var nav = tab.getElementsByTagName("nav")[0];
    var ul = nav.getElementsByTagName("ul")[0];
    // var li = ul.getElementsByTagName("li");
    if(){
        // 判断tab标签
    }
    var li = document.createElement("li");
    var a = document.createElement("a");
    nav.setAttribute("class", "show");
    li.setAttribute("class", "orderInline");
    a.setAttribute("href", "javaScript:void(0)");
    a.setAttribute("onclick","changeTab(this)")
    a.innerText = obj.innerText;
    li.appendChild(a);
    ul.appendChild(li);
    context.innerHTML = "";
    loadContext(url, obj.parentNode.id);
}

function loadContext(url,divId) {
    var main = document.getElementById("main");
    var contentDiv = createElement("div","","","show");
    contentDiv.innerHTML = "";
    contentDiv.innerText = url+divId;
    main.appendChild(contentDiv);
    for(var i=0;i<main.getElementsByTagName("div").length;i++){
        if(main.getElementsByTagName("div")[i].className == "show"&&main.getElementsByTagName("div")[i] != contentDiv){
            main.getElementsByTagName("div")[i].setAttribute("class","hidden");
        }
    }

    // for(var i=0;i<main.getElementsByTagName("div").length;i++){
    //     if(main.getElementsByTagName("div")[i].className == "currentTab"){
    //         main.getElementsByTagName("div")[i].setAttribute("class","hidden");
    //     }
    // }
    // divObj.innerHTML = "";
    //divObj.innerText = url;
    // divObj.innerText = divId;

}

/**
 * 创建一个DOM元素
 * @param elementName 标签名称
 * @param type 元素类型
 * @param id 元素ID
 * @param className 元素class
 * @returns {Element}
 */
function createElement(elementName,type,id,className){
    var element = document.createElement(elementName);
    element.setAttribute("type",type);
    element.setAttribute("id",id);
    element.setAttribute("class",className);
    return element;
}
window.onload = function(){
    getMenu();
}